const util = require('../../utils/util.js')
var time_check = new Array();//第一位表示星期，第二位开始表示时间段
var info_check = new Array();
var stunum =1;
var app = getApp();
const DB = wx.cloud.database().collection('data')
const info_DB = wx.cloud.database().collection('info_data')

var collection=[
  "classroom_state_Sun",
  "classroom_state_Mon",
  "classroom_state_Tue",
  "classroom_state_Wed",
  "classroom_state_Thu",
  "classroom_state_Fri",
  "classroom_state_Sat",
]

Page({
  data: {
    time:["星期一","星期二","星期三","星期四","星期五","星期六","星期天"],
    disabled:[
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
    ],
    time_date:[],
    key:[
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
      0,0,0,0,0,0,0,
    ],
    button:[
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{},
      {},{},{},{},{},{},{}
    ],
    day:[{},{},{},{},{},{},{}],
    logs: [],
    day1: "1",
    day2: "1",
    day3: "1",
    day4: "1",
    day5: "1",
    day6: "1",
    day7: "1",
    week1: "1",
    week2: "1",
    week3: "1",
    week4: "1",
    week5: "1",
    week6: "1",
    week7: "1",
    cause: "",
    departName: ""
  },
  onLoad(){
    let that = this;
    wx.getStorage({
      key: 'number', //名字
      success: function(res) {
        stunum = Number(res.data)
      }, //取出来之后的回调函数
    });
    var now = new Date();
    var t = 0;
    var hh = now.getHours();
    var mm = now.getMinutes();
    t = (hh-8)*2+parseInt(mm/30);
    if(t>28){t=28;}
    for(var j=0;j<t;j++){
      let disabled_temp = 'disabled['+(j)+']';
      let key_temp = 'key['+(j)+']';
      console.log(key_temp)
      that.setData({
        [disabled_temp]:1,
        [key_temp]:4
      })
    }
    let key_temp_1 = 'key['+(t)+']';
    this.setData({
      [key_temp_1]:5,
    })
    console.log(that.data.key,this.data.disabled)
    let bb = now.getDay(); 
    for(let i=0;i<7;i++){
      DB.doc(collection[bb]).get().then(res=>{
        for(let jj=0;jj<28;jj++){
          if(res.data[jj]==stunum){
            let key_temp = 'key['+(i*28+jj)+']';
            if(that.data.key[i*28+jj]<4){
              that.setData({
                [key_temp]:3
              });
            }
          }
          else if(res.data[jj]!=0){
            let key_temp = 'key['+(i*28+jj)+']';
            if(that.data.key[i*28+jj]<4){
              that.setData({
                [key_temp]:2
              });
            }
          }
        }
      });
      bb=bb+1;
      if(bb==7){
        bb=0;
      }
    }
    var arr_half = new Array();
    var arr_whole = new Array();
    var arr = new Array();
    for(var i =0;i<14;i++){
      arr_half[i] = (8+i)+":"+"00 ~ "+(8+i)+":"+"30";
      arr_whole[i] = (8+i)+":"+"30 ~ "+(9+i)+":"+"00";
    }
    var h = 0;
    var w = 0;
    for(var i=0;i<196;i++){
      time_check[i]=0;
      info_check[i+1]="0";
    }
    for(var i =0;i<28;i++){
      
      if(i%2){
        arr[i]=arr_whole[h];
        h++;
      }
      else{
        arr[i]=arr_half[w];
        w++;
      }
    }
    this.setData({
      time_date:arr
    }
    );
    var weekList = app.getWeekTime();
    this.setData({day1 : weekList[0].monthDate,
      week1 : weekList[0].weekDate,
      day2 : weekList[1].monthDate,
      week2 : weekList[1].weekDate,
      day3 : weekList[2].monthDate,
      week3 : weekList[2].weekDate,
      day4 : weekList[3].monthDate,
      week4 : weekList[3].weekDate,
      day5 : weekList[4].monthDate,
      week5 : weekList[4].weekDate,
      day6 : weekList[5].monthDate,
      week6 : weekList[5].weekDate,
      day7 : weekList[6].monthDate,
      week7 : weekList[6].weekDate})
  },
  apply(){
    let j=0;
    let that = this;
    for(let i=0;i<196;i++){
      if(time_check[i]!=0){
        j++;
      }
    }
    if(j==0){
      wx.showToast({
        title: '你未选择时间段',
        icon: 'none',
        duration: 1000,
      }) 
      that.onLoad();
      return;
    }
    if(stunum==null || stunum==1){
      console.log(stunum)
      wx.showToast({
        title: '请在个人信息栏中填写学号和手机号',
        icon: 'none',
        duration: 1000,
      })  
      return;
    }
    this.setData({
      ifApply:1
    });
  },
  confirm(){
    let that = this;
    if(this.data.cause=="" || this.data.departName==""){
      wx.showToast({
        title: '请完善相应信息',
        icon: 'none',
        duration: 1000,
      })
      that.setData({
        ifApply:0
      });
      return;
    }
    info_check[0]=stunum;
    var temp_j = new Array();
    let temp_i = 0;
    var day = new Date();
    let bb = day.getDay();
    let t=0;
    for(let i = 0; i < 196; i++){
      if(time_check[i]==1){
        info_check[1+(i+28*bb)%196]="机构: "+that.data.departName+"\r\n"+"用途: "+that.data.cause;
      }
    }
    console.log(info_check)
    wx.cloud.callFunction({
      name:"reserve_room",
      data:{
        stu_array:info_check
      },
    }).then(res => {
      wx.showToast({
        title: '申请成功',
        icon: 'none',
        duration: 1000,
      })  
      that.setData({
        ifApply:0
      });
      that.onLoad();
    })
  },
  choose(e){
    var i = Number(e.currentTarget.dataset.i);
    let o = 0;
    this.timedel = (this.endTime - this.startTime);
    if (this.timedel < 300 && (this.data.key[i]!=2 ||this.data.key[i]!=3)) {      
      let key_temp = 'key['+i+']';
      if(this.data.key[i]==2){
        return;
      }
      if(this.data.key[i]==3){
        return;
      }
      else if(this.data.key[i]==1){
        this.setData({
          [key_temp]:0
        });
        time_check[i]=0;     
      }
      else{
        this.setData({
          [key_temp]:1
        });
        time_check[i]=1;
        for(let i = 0; i < 196; i++){
          if(time_check[i]==1){
            o++;
          }
          if(o>=5){
            wx.showToast({
              title: '建议只选择五个时间段',
              icon: 'none',
              duration: 1000,
            })
          }
        }
      }
    }
  },
  setcause:function(e){
    this.setData({
      cause: e.detail.value    
    })
  },
  setdepartName:function(e){
    this.setData({
      departName: e.detail.value    
    })
  },
  handleLongPress(e){
    let i = Number(e.currentTarget.dataset.i);
    var day = new Date();
    let bb = day.getDay();
    let temp_i = 0;
    let that = this;
    temp_i = (parseInt(i/28)+bb)%7;
    let a = "";
    info_DB.doc(collection[temp_i]).get().then(res=>{
      a=res.data[i%28];
      if(a=="0"){
        return;
      }
      if(that.data.key[i]==3){
        time_check[i]=-1;
        that.setData({
          ifCancel:1,
          your_apply:a
        })
      }
      if(that.data.key[i]==2){
        wx.showToast({
          title: a,
          icon: 'none',
          duration: 1000,
        })
      }
    });
  },
  handleTouchEnd(e) {    
    this.endTime=e.timeStamp;
  },
  handleTouchStart(e) {    
    this.startTime=e.timeStamp;
  },  
  cancel(){
    this.setData({
      ifApply:0
    })
  },
  cancel_your(){
    this.setData({
      ifCancel:0
    })
  },
  confirm_your(){
    let i = 0;
    for(let j=0;j<196;j++){
      if(time_check[j]==-1){
        time_check[j]=0;
        i=j;
        break;
      }
    }
    var day = new Date();
    let bb = day.getDay();
    let temp_i = 0;
    let that = this;
    temp_i = (parseInt(i/28)+bb)%7;
    let a = i%28+"";
    info_DB.doc(collection[temp_i]).update({
      data: {
        [a]: "0"
      }
    })
    DB.doc(collection[temp_i]).update({
      data: {
        [a]: 0
      }
    })
    wx.showToast({
      title: '已成功取消',
      icon: 'none',
      duration: 1000,
    })
    this.setData({
      ifCancel:0
    })
    let key_temp = 'key['+i+']';
    this.setData({
      [key_temp]:0
    })
  }
})